WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
jet_traits.h
Go to the documentation of this file.
1// This is not part of WOLF
2// clang-format off
3//
4// Adapted from Ceres Solver 2.2.0 (include/ceres/internal/jet_traits.h) so that WOLF autodiff does not
5// require Ceres: namespace 'ceres' renamed to 'wolf::autodiff', and 'CERES_' macros and include
6// guards renamed to 'WOLF_AUTODIFF_'. The original license follows.
7//
8// Ceres Solver - A fast non-linear least squares minimizer
9// Copyright 2023 Google Inc. All rights reserved.
10// http://ceres-solver.org/
11//
12// Redistribution and use in source and binary forms, with or without
13// modification, are permitted provided that the following conditions are met:
14//
15// * Redistributions of source code must retain the above copyright notice,
16// this list of conditions and the following disclaimer.
17// * Redistributions in binary form must reproduce the above copyright notice,
18// this list of conditions and the following disclaimer in the documentation
19// and/or other materials provided with the distribution.
20// * Neither the name of Google Inc. nor the names of its contributors may be
21// used to endorse or promote products derived from this software without
22// specific prior written permission.
23//
24// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34// POSSIBILITY OF SUCH DAMAGE.
35//
36// Author: sergiu.deitsch@gmail.com (Sergiu Deitsch)
37//
38
39#ifndef WOLF_AUTODIFF_PUBLIC_INTERNAL_JET_TRAITS_H_
40#define WOLF_AUTODIFF_PUBLIC_INTERNAL_JET_TRAITS_H_
41
42#include <tuple>
43#include <type_traits>
44#include <utility>
45
48
49namespace wolf::autodiff {
50namespace internal {
51
52// Predicate that determines whether any of the Types is a Jet.
53template <typename... Types>
54struct AreAnyJet : std::false_type {};
55
56template <typename T, typename... Types>
57struct AreAnyJet<T, Types...> : AreAnyJet<Types...> {};
58
59template <typename T, int N, typename... Types>
60struct AreAnyJet<Jet<T, N>, Types...> : std::true_type {};
61
62// Convenience variable template for AreAnyJet.
63template <typename... Types>
64inline constexpr bool AreAnyJet_v = AreAnyJet<Types...>::value;
65
66// Extracts the underlying floating-point from a type T.
67template <typename T, typename E = void>
69 using type = T;
70};
71
72template <typename T, int N>
74
75// Convenience template alias for UnderlyingScalar type trait.
76template <typename T>
78
79// Predicate determining whether all Types in the pack are the same.
80//
81// Specifically, the predicate applies std::is_same recursively to pairs of
82// Types in the pack.
83template <typename T1, typename... Types>
84inline constexpr bool AreAllSame_v = (std::is_same<T1, Types>::value && ...);
85
86// Determines the rank of a type. This allows to ensure that types passed as
87// arguments are compatible to each other. The rank of Jet is determined by the
88// dimensions of the dual part. The rank of a scalar is always 0.
89// Non-specialized types default to a rank of -1.
90template <typename T, typename E = void>
91struct Rank : std::integral_constant<int, -1> {};
92
93// The rank of a scalar is 0.
94template <typename T>
95struct Rank<T, std::enable_if_t<std::is_scalar<T>::value>>
96 : std::integral_constant<int, 0> {};
97
98// The rank of a Jet is given by its dimensionality.
99template <typename T, int N>
100struct Rank<Jet<T, N>> : std::integral_constant<int, N> {};
101
102// Convenience variable template for Rank.
103template <typename T>
104inline constexpr int Rank_v = Rank<T>::value;
105
106// Constructs an integer sequence of ranks for each of the Types in the pack.
107template <typename... Types>
108using Ranks_t = std::integer_sequence<int, Rank_v<Types>...>;
109
110// Returns the scalar part of a type. This overload acts as an identity.
111template <typename T>
112constexpr decltype(auto) AsScalar(T&& value) noexcept {
113 return std::forward<T>(value);
114}
115
116// Recursively unwraps the scalar part of a Jet until a non-Jet scalar type is
117// encountered.
118template <typename T, int N>
119constexpr decltype(auto) AsScalar(const Jet<T, N>& value) noexcept(
120 noexcept(AsScalar(value.a))) {
121 return AsScalar(value.a);
122}
123
124} // namespace internal
125
126// Type trait ensuring at least one of the types is a Jet,
127// the underlying scalar types are the same and Jet dimensions match.
128//
129// The type trait can be further specialized if necessary.
130//
131// This trait is a candidate for a concept definition once C++20 features can
132// be used.
133template <typename... Types>
134// clang-format off
135struct CompatibleJetOperands : std::integral_constant
136<
137 bool,
138 // At least one of the types is a Jet
139 internal::AreAnyJet_v<Types...> &&
140 // The underlying floating-point types are exactly the same
141 internal::AreAllSame_v<internal::UnderlyingScalar_t<Types>...> &&
142 // Non-zero ranks of types are equal
143 internal::IsEmptyOrAreAllEqual_v<internal::RemoveValue_t<internal::Ranks_t<Types...>, 0>>
144>
145// clang-format on
146{
147};
148
149// Single Jet operand is always compatible.
150template <typename T, int N>
151struct CompatibleJetOperands<Jet<T, N>> : std::true_type
152{
153};
154
155// Single non-Jet operand is always incompatible.
156template <typename T>
157struct CompatibleJetOperands<T> : std::false_type
158{
159};
160
161// Empty operands are always incompatible.
162template <>
163struct CompatibleJetOperands<> : std::false_type
164{
165};
166
167// Convenience variable template ensuring at least one of the types is a Jet,
168// the underlying scalar types are the same and Jet dimensions match.
169//
170// This trait is a candidate for a concept definition once C++20 features can
171// be used.
172template <typename... Types>
173inline constexpr bool CompatibleJetOperands_v = CompatibleJetOperands<Types...>::value;
174
175// Type trait ensuring at least one of the types is a Jet,
176// the underlying scalar types are compatible among each other and Jet
177// dimensions match.
178//
179// The type trait can be further specialized if necessary.
180//
181// This trait is a candidate for a concept definition once C++20 features can
182// be used.
183template <typename... Types>
184// clang-format off
185struct PromotableJetOperands : std::integral_constant
186<
187 bool,
188 // Types can be compatible among each other
189 internal::AreAnyJet_v<Types...> &&
190 // Non-zero ranks of types are equal
191 internal::IsEmptyOrAreAllEqual_v<internal::RemoveValue_t<internal::Ranks_t<Types...>, 0>>
192>
193// clang-format on
194{
195};
196
197// Convenience variable template ensuring at least one of the types is a Jet,
198// the underlying scalar types are compatible among each other and Jet
199// dimensions match.
200//
201// This trait is a candidate for a concept definition once C++20 features can
202// be used.
203template <typename... Types>
204inline constexpr bool PromotableJetOperands_v = PromotableJetOperands<Types...>::value;
205
206} // namespace wolf::autodiff
207
208#endif // WOLF_AUTODIFF_PUBLIC_INTERNAL_JET_TRAITS_H_
Definition: jet.h:1355
std::integer_sequence< int, Rank_v< Types >... > Ranks_t
Definition: jet_traits.h:108
typename UnderlyingScalar< T >::type UnderlyingScalar_t
Definition: jet_traits.h:77
constexpr decltype(auto) AsScalar(T &&value) noexcept
Definition: jet_traits.h:112
constexpr bool AreAnyJet_v
Definition: jet_traits.h:64
constexpr int Rank_v
Definition: jet_traits.h:104
constexpr bool AreAllSame_v
Definition: jet_traits.h:84
Definition: integer_sequence_algorithm.h:50
constexpr bool CompatibleJetOperands_v
Definition: jet_traits.h:173
constexpr bool PromotableJetOperands_v
Definition: jet_traits.h:204
Definition: jet_traits.h:146
Definition: jet.h:218
T a
Definition: jet.h:293
Definition: jet_traits.h:194
Definition: jet_traits.h:54
Definition: jet_traits.h:91
T type
Definition: jet_traits.h:69